package com.jh3502.helper.function.person.personsp;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.TextView;

import com.jh3502.helper.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author harisucici
 */
public class PersonSpAdapter extends RecyclerView.Adapter<PersonSpAdapter.ViewHolder> {


    private List<PersonSpBean> data;
    private Context context;

    private OnRecyclerViewItemClickListener mOnItemClickListener;

    public PersonSpAdapter(Context context, List<PersonSpBean> data) {
        this.context = context;
        this.data = data;

    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ViewHolder holder = new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_person_sp, parent, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int position) {

        final PersonSpBean label = data.get(position);
        viewHolder.itemPersonSpTitle.setText(label.getName());
//        viewHolder.item_person_sp_radio.setText(label.getName());
        viewHolder.itemPersonSpRadio.setId(position);
        if (label.getIsCheck()) {
            viewHolder.itemPersonSpRadio.setChecked(true);
        } else {
            viewHolder.itemPersonSpRadio.setChecked(false);
        }

        View.OnClickListener clickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemClick(viewHolder.itemView, position, label.getIsCheck());
            }
        };
        viewHolder.itemView.setOnClickListener(clickListener);
        viewHolder.itemPersonSpRadio.setOnClickListener(clickListener);
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public interface OnRecyclerViewItemClickListener {

        void onItemClick(View view, int position, boolean isCheck);
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_person_sp_title)
        TextView itemPersonSpTitle;
        @BindView(R.id.item_person_sp_radio)
        RadioButton itemPersonSpRadio;

        public ViewHolder(@NonNull final View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

}

